package com.shipwe.loans.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.shipwe.adapter.ShipweRecyclerViewAdapter
import com.shipwe.adapter.ShipweViewHolderHelper
import com.shipwe.loans.MyApps
import com.shipwe.loans.R
import com.shipwe.loans.utils.AppLoadImageUtil
import org.json.JSONObject
import java.math.BigDecimal

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/13 10:55
 * @Annotation：首页产品列表
 */
class ProductAdapter(recyclerView: RecyclerView?) : ShipweRecyclerViewAdapter<JSONObject>(recyclerView) {

    private val TYPE_PRODUCT = "TYPE_PRODUCT"//产品
    private val TYPE_PRODUCT_APPLIED = "TYPE_PRODUCT_APPLIED"//已申请产品
    private val TYPE_NO_MORE = "TYPE_NO_MORE"//No more
    private val TYPE_APPLIED_EMPTY = "TYPE_APPLIED_EMPTY"//applied Empty
    private val TYPE_PRODUCT_EMPTY = "TYPE_PRODUCT_EMPTY"//product Empty

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        return when(model.optString("itemType")){
            TYPE_NO_MORE -> {
                R.layout.product_item_no_more_data
            }
            TYPE_APPLIED_EMPTY,
            TYPE_PRODUCT_EMPTY -> {
                R.layout.product_item_empty
            }
            TYPE_PRODUCT_APPLIED -> {//已申请的产品
                R.layout.product_item_applied
            }
            else -> {//产品
                R.layout.product_item
            }
        }
    }

    override fun setItemChildListener(helper: ShipweViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.productApply)//申请
        helper.setItemChildClickListener(R.id.productEmptyText)//去申请
        helper.setItemChildClickListener(R.id.productReviewing)//审核中
        helper.setItemChildClickListener(R.id.productDisbursing)//放款中
        helper.setItemChildClickListener(R.id.productRejected)//已拒绝
    }

    override fun fillData(helper: ShipweViewHolderHelper, position: Int, model: JSONObject) {
        when(model.optString("itemType")){
            TYPE_NO_MORE -> {
            }
            TYPE_APPLIED_EMPTY,
            TYPE_PRODUCT_EMPTY -> {
                showHomeProductEmptyView(helper, position, model)
            }
            TYPE_PRODUCT_APPLIED -> {
                showHomeProductAppliedView(helper, position, model)
            }
            else -> {
                showHomeProductView(helper, position, model)
            }
        }
    }

    /**
     * 没有产品提示
     */
    private fun showHomeProductEmptyView(helper: ShipweViewHolderHelper, position: Int, model: JSONObject) {
        val productEmptyText = helper.getTextView(R.id.productEmptyText)
        var showKey = MyApps.app.getString(R.string.product_empty_apply)
        if(model.optString("itemType") == TYPE_PRODUCT_EMPTY){
            showKey = MyApps.app.getString(R.string.product_empty_repay)
        }
        val showText = String.format(MyApps.app.getString(R.string.product_empty_tips),showKey)
        val spString = SpannableString(showText)
        val start = showText.indexOf(showKey)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#000000")), start, start+showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //添加下划线
        spString.setSpan(UnderlineSpan(), start, showText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        productEmptyText.text = spString
    }

    /**
     * 产品
     */
    @SuppressLint("SetTextI18n")
    private fun showHomeProductView(helper: ShipweViewHolderHelper, position: Int, model: JSONObject){
        val productLogo = helper.getImageView(R.id.productLogo)
        AppLoadImageUtil.instance.loadImageUrl(mContext,productLogo,model.optString("logo"),R.drawable.load_holder)
        helper.setText(R.id.productName,model.optString("name"))

        val productAmount = helper.getTextView(R.id.productAmount)
        val showAmount = BigDecimal(model.optString("amount")).stripTrailingZeros().toPlainString()
        productAmount.text = "${model.optString("currency")} $showAmount"

        when(model.optString("activeFlag")){
            "YES" -> {//已解锁
                helper.setVisibility(R.id.productLock,View.GONE)
            }
            else -> {//未解锁
                helper.setVisibility(R.id.productLock,View.VISIBLE)
            }
        }
    }

    /**
     * 已申请的产品
     */
    @SuppressLint("SetTextI18n")
    private fun showHomeProductAppliedView(helper: ShipweViewHolderHelper, position: Int, model: JSONObject){
        val productLogo = helper.getImageView(R.id.productLogo)
        AppLoadImageUtil.instance.loadImageUrl(mContext,productLogo,model.optString("logo"),R.drawable.load_holder)
        helper.setText(R.id.productName,model.optString("name"))

        val productAmount = helper.getTextView(R.id.productAmount)
        val showAmount = BigDecimal(model.optString("amount")).stripTrailingZeros().toPlainString()
        productAmount.text = "${model.optString("currency")} $showAmount"

        // 使用BigDecimal来保存数值
        val bigDecimalValue = BigDecimal(model.optString("interestRate")).multiply(BigDecimal("100"))
        val showRate = bigDecimalValue.stripTrailingZeros().toPlainString()
        helper.setText(R.id.productInterestRate,"$showRate%")

        val applyStatus = model.optString("applyStatus")
        val status = model.optString("status")

        helper.setVisibility(R.id.productReviewing,View.GONE)
        helper.setVisibility(R.id.productDisbursing,View.GONE)
        helper.setVisibility(R.id.productRejected,View.GONE)
        if (applyStatus.endsWith("DENIED")){//被拒绝
            helper.setVisibility(R.id.productRejected,View.VISIBLE)
            helper.setText(R.id.productReapplyTime,model.optString("againApplyDate"))

        } else {
            if (status == "PENDING"){
                if (applyStatus == "SUCCEEDED"){//放款中
                    helper.setVisibility(R.id.productDisbursing,View.VISIBLE)
                } else {//审核中
                    helper.setVisibility(R.id.productReviewing,View.VISIBLE)
                }
            } else {
                helper.setVisibility(R.id.productReviewing,View.VISIBLE)
            }
        }
    }
}